home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / misc / xmgr_docs.lha / xmgr_docs / aux / jul2greg.c < prev   
Encoding:
C/C++ Source or Header  |  1993-05-02  |  3.1 KB  |  134 lines

  1. /*
  2.  * convert Julian date to Gregorian
  3.  *
  4.  * Compile with 'cc jul2greg.c -o jul2greg'
  5.  *
  6.  * Modify as needed for your application.
  7.  *
  8.  * The Julian day starts at noon of the Gregorian day and extends
  9.  * to noon the next Gregorian day. The Gregorian day is assumed
  10.  * to begin at midnight.
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <math.h>
  16.  
  17. double julday();
  18.  
  19. char *dayofweekstr[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  20.  
  21. main(argc, argv)
  22.     int argc;
  23.     char **argv;
  24. {
  25.     int cnt, m, d, y, h = 12, mi = 0;
  26.     double se, data;
  27.     double j;
  28.     char s[256];
  29.  
  30.     while (gets(s) != NULL) {
  31.     sscanf(s, "%lf", &j);
  32.     calcdate(j, &m, &d, &y, &h, &mi, &se);
  33.         printf("%d %d %d %d %d %lf %s \n", m, d, y, h, mi, se, dayofweekstr[dayofweek(j)]);
  34.     }
  35. }
  36.  
  37. /*
  38. ** Takes a date, and returns a Julian day. A Julian day is the number of
  39. ** days since some base date  (in the very distant past).
  40. ** Handy for getting date of x number of days after a given Julian date
  41. ** (use jdate to get that from the Gregorian date).
  42. ** Author: Robert G. Tantzen, translator: Nat Howard
  43. ** Translated from the algol original in Collected Algorithms of CACM
  44. ** (This and jdate are algorithm 199).
  45. */
  46. double julday(mon, day, year, h, mi, se)
  47.     int mon, day, year, h, mi;
  48.     double se;
  49. {
  50.     long m = mon, d = day, y = year;
  51.     long c, ya, j;
  52.     double seconds = h * 3600.0 + mi * 60 + se;
  53.  
  54.     if (m > 2)
  55.     m -= 3;
  56.     else {
  57.     m += 9;
  58.     --y;
  59.     }
  60.     c = y / 100L;
  61.     ya = y - (100L * c);
  62.     j = (146097L * c) / 4L + (1461L * ya) / 4L + (153L * m + 2L) / 5L + d + 1721119L;
  63.     if (seconds < 12 * 3600.0) {
  64.     j--;
  65.     seconds += 12.0 * 3600.0;
  66.     }
  67.     else {
  68.     seconds = seconds - 12.0 * 3600.0;
  69.     }
  70.     return (j + (seconds / 3600.0) / 24.0);
  71. }
  72.  
  73. /* Julian date converter. Takes a julian date (the number of days since
  74. ** some distant epoch or other), and returns an int pointer to static space.
  75. ** ip[0] = month;
  76. ** ip[1] = day of month;
  77. ** ip[2] = year (actual year, like 1977, not 77 unless it was  77 a.d.);
  78. ** ip[3] = day of week (0->Sunday to 6->Saturday)
  79. ** These are Gregorian.
  80. ** Copied from Algorithm 199 in Collected algorithms of the CACM
  81. ** Author: Robert G. Tantzen, Translator: Nat Howard
  82. */
  83. calcdate(jd, m, d, y, h, mi, sec)
  84.     double jd;
  85.     int *m, *d, *y, *h, *mi;
  86.     double *sec;
  87. {
  88.     static int ret[4];
  89.  
  90.     long j = jd;
  91.     double tmp, frac = jd - j;
  92.     if (frac >= 0.5) {
  93.     frac = frac - 0.5;
  94.         j++;
  95.     }
  96.     else {
  97.     frac = frac + 0.5;
  98.     }
  99.  
  100.     ret[3] = (j + 1L) % 7L;
  101.     j -= 1721119L;
  102.     *y = (4L * j - 1L) / 146097L;
  103.     j = 4L * j - 1L - 146097L * *y;
  104.     *d = j / 4L;
  105.     j = (4L * *d + 3L) / 1461L;
  106.     *d = 4L * *d + 3L - 1461L * j;
  107.     *d = (*d + 4L) / 4L;
  108.     *m = (5L * *d - 3L) / 153L;
  109.     *d = 5L * *d - 3 - 153L * *m;
  110.     *d = (*d + 5L) / 5L;
  111.     *y = 100L * *y + j;
  112.     if (*m < 10)
  113.     *m += 3;
  114.     else {
  115.     *m -= 9;
  116.     *y += 1;
  117.     }
  118.     tmp = 3600.0 * (frac * 24.0);
  119.     *h = (int) (tmp / 3600.0);
  120.     tmp = tmp - *h * 3600.0;
  121.     *mi = (int) (tmp / 60.0);
  122.     *sec = tmp - *mi * 60.0;
  123. }
  124.  
  125. int dayofweek(j)
  126.     double j;
  127. {
  128.     long jd = (long) j;
  129.     if (j - jd >= 0.5) {
  130.     jd++;
  131.     }
  132.     return (jd + 1) % 7;
  133. }
  134.